package com.tools.payhelper.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @Description: 日期格式类
 * @date 2015-9-16 上午11:47:52 
 * @author mars.chen 
 */
public class DateUtils {
	
	static final String formatPattern = "yyyy-MM-dd";
	
	static final String formatPattern_Short = "yyyyMMdd";
	
	static final String formatPattern_Long = "yyyy-MM-dd HH:mm:ss";
	
	/**
	 * @Description: 获取当前日期
	 * @return String
	 */
	public static String getCurrentDate(){
		SimpleDateFormat format = new SimpleDateFormat(formatPattern);
		return format.format(new Date());
	}
	
	/**
	 * @Description: 获取当前日期,截止到毫秒
	 * @return String
	 */
	public static String getCurrentLongDate(){
		SimpleDateFormat format = new SimpleDateFormat(formatPattern_Long);
		return format.format(new Date());
	}
	
	/**
	 * @Description: 获取制定毫秒数之前的日期
	 * @param timeDiff
	 * @return String
	 */
	public static String getDesignatedDate(long timeDiff){
		SimpleDateFormat format = new SimpleDateFormat(formatPattern);
		long nowTime = System.currentTimeMillis();
		long designTime = nowTime - timeDiff;		
		return format.format(designTime);
	}
	
	/**
	 * @Description: 获取前几天的日期
	 * @return String
	 */
	public static String getPrefixDate(String count){
		Calendar cal = Calendar.getInstance();
		int day = 0- Integer.parseInt(count);
		cal.add(Calendar.DATE,day);   // int amount   代表天数
		Date datNew = cal.getTime();
		SimpleDateFormat format = new SimpleDateFormat(formatPattern);
		return format.format(datNew);
	}
	/**
	 * @Description: 日期转换成字符串
	 * @param date
	 * @return String
	 */
	public static String dateToString(Date date){
		SimpleDateFormat format = new SimpleDateFormat(formatPattern);
		return format.format(date);
	}
	
	/**
	 * @Description: 当前日期时间转换成字符串 yyyy-MM-dd HH:mm:ss
	 * @param date
	 * @return String
	 */
	public static String dateTimeToString(Date date){
		SimpleDateFormat format = new SimpleDateFormat(formatPattern_Long);
		return format.format(date);
	}
	
	/**
	 * @Description: 字符串转换日期
	 * @param str
	 * @return Date
	 */
	public static Date stringToDate(String str){
		//str =  " 2008-07-10 19:20:00 " 格式
		SimpleDateFormat format = new SimpleDateFormat(formatPattern);
		if(!str.equals("")&&str!=null){
			try {
				return format.parse(str);
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

}
